﻿/// ---------------------------------------------
///
/// ---------------------------------------------

///---------------------------
///2012.4.30 汪定新添加
///身份证号码验证
function CheckIDCard(idcard) {
    var Errors = new Array(
                 "0",
                 "身份证号码位数不对!",
                 "身份证号码出生日期超出范围或含有非法字符!",
                 "身份证号码校验错误!",
                  "身份证地区非法!");
    var area = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" }
    var idcard, Y, JYM;
    var S, M;
    var idcard_array = new Array();
    idcard_array = idcard.split("");
    //地区检验 
    if (area[parseInt(idcard.substr(0, 2))] == null) return Errors[4];
    //身份号码位数及格式检验 
    switch (idcard.length) {
        case 15:
            if ((parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0 || ((parseInt(idcard.substr(6, 2)) + 1900) % 100 == 0 && (parseInt(idcard.substr(6, 2)) + 1900) % 4 == 0)) {
                ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/; //测试出生日期的合法性 
            } else {
                ereg = /^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/; //测试出生日期的合法性 
            }
            if (ereg.test(idcard)) return Errors[0];
            else return Errors[2];
            break;
        case 18:
            //18位身份号码检测 
            //出生日期的合法性检查  
            //闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9])) 
            //平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8])) 
            if (parseInt(idcard.substr(6, 4)) % 4 == 0 || (parseInt(idcard.substr(6, 4)) % 100 == 0 && parseInt(idcard.substr(6, 4)) % 4 == 0)) {
                ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/; //闰年出生日期的合法性正则表达式 
            } else {
                ereg = /^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/; //平年出生日期的合法性正则表达式 
            }
            if (ereg.test(idcard)) {//测试出生日期的合法性 
                //计算校验位 
                S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
                            + (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
                            + (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
                            + (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
                            + (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
                            + (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
                            + (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
                            + parseInt(idcard_array[7]) * 1
                            + parseInt(idcard_array[8]) * 6
                            + parseInt(idcard_array[9]) * 3;
                Y = S % 11;
                M = "F";
                JYM = "10X98765432";
                M = JYM.substr(Y, 1); //判断校验位 
                if (M == idcard_array[17]) return Errors[0]; //检测ID的校验位 
                else return Errors[3];
            }
            else return Errors[2];
            break;
        default:
            return Errors[1];
            break;
    }
}
//验证台湾身份证
function checkTaiWanIdCard(idnum) {
    num = idnum;
    num = num.toLowerCase();
    patten = /^[a-z][12][0-9]{8}$/;
    if (patten.test(num)) {
        h = "abcdefghjklmnpqrstuvxywzio ";
        x = 10 + h.indexOf(num.substring(0, 1));
        chksum = (x - (x % 10)) / 10 + (x % 10) * 9;
        for (i = 1; i < 9; i++) {
            chksum += num.substring(i, i + 1) * (9 - i);
        }
        chksum = (10 - chksum % 10) % 10;
        if (chksum == num.substring(9, 10)) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}


//根据身份证号获取出生日期
function getBirthdatByIdNo(iIdNo) {
    var tmpStr = "";
    var idDate = "";
    var tmpInt = 0;
    var strReturn = "";
    var no = iIdNo.replace(/^\s+|\s+$/g, ""); ;
    if ((no.length != 15) && (no.length != 18)) {
        return strReturn;
    }
    if (no.length == 15) {
        tmpStr = no.substring(6, 12);
        tmpStr = "19" + tmpStr;
        tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)
        return tmpStr;
    }
    else {
        tmpStr = iIdNo.substring(6, 14);
        tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)
        return tmpStr;
    }
}
//根据身份证获取性别
function getSexByIdNo(iIdNo) {
    var tmpStr = "";
    var idDate = "";
    var tmpInt = 0;
    var strReturn = "";
    var no = iIdNo.replace(/^\s+|\s+$/g, ""); ;
    if ((no.length != 15) && (no.length != 18)) {
        return strReturn;
    }
    if (no.length == 15) {
        if (parseInt(no.charAt(14) / 2) * 2 != no.charAt(14)) {
            return "男";
        }
        else {
            return "女";
        }
    }
    else {
        if (parseInt(no.charAt(16) / 2) * 2 != no.charAt(16)) {
            return "男";
        }
        else {
            return "女";
        }
    }
}

//根据出生日期获取年龄
function getAgeByBirthday(birthdayValue) {
    var dt1 = new Date(birthdayValue.replace("-", "/"));
    var dt2 = new Date();
    var age = dt2.getFullYear() - dt1.getFullYear();
    var m = dt2.getMonth() - dt1.getMonth();
    if (m < 0)
        age--;
    return age;
}

var fnAddEduHistory = function () {
}

var fnChekcIDCard = function (idcard) {
    $.get("", function (data, status) { })
}

function setEmployeeType() {
    //根据入职日期和试用期限判断员工类型,试用期还是正式
    var qixianText = $("#ProbationPeriod").find("option:selected").text();
    if (qixianText == "无") { //无试用期直接为正式员工
        instance[0].EmployeeType = "D924C147-1A23-4FBE-A958-2F58FD9377D7";
    } else {
        var JoinDate = $("#JoinDate").val(); //入职日期
        var time = new Date(Date.parse(JoinDate.replace(/-/g, "/")));
        time.setMonth(parseInt(time.getMonth()) + parseInt(qixianText.substr(0, 1)));
        var now = new Date();
        y = now.getFullYear();
        m = now.getMonth() + 1;
        d = now.getDate();
        m = m < 10 ? "0" + m : m;
        d = d < 10 ? "0" + d : d;
        var myDate = y + "-" + m + "-" + d;
        var yy = time.getFullYear()
        var mm = time.getMonth() + 1;
        var dd = time.getDate();
        mm = mm < 10 ? "0" + mm : mm;
        dd = dd < 10 ? "0" + dd : dd;
        var tempDate = yy + "-" + mm + "-" + dd;
        if (tempDate < myDate) { //试用期过 正式员工
            instance[0].EmployeeType = "D924C147-1A23-4FBE-A958-2F58FD9377D7";
        }
        else {//试用期内
            instance[0].EmployeeType = "8D7C895F-1DAD-4C72-A299-1EC9C9D13BC3";
        }
    }
    //
}

if (typeof ($) != "undefined") {
    $(document).ready(function () {
        $("#IDCardNO").change(function () { //输入身份证后,自动显示出生日期
            var cardNo = $("#IDCardNO").val();
            if (cardNo.length >= 15) {
                var str = getBirthdatByIdNo(cardNo);
                if (str != "") {
                    $("#BirthDate").val(str);
                    $("#age").val(getAgeByBirthday(str)); //年龄
                }
                var sex = getSexByIdNo(cardNo);
                if (sex != "") {
                    $("#Gender").attr("text", sex); //性别
                }
            }
        });
        $("#ProbationPeriod").change(function () {//试用期限和员工类型设定
            var shiyongQi = $("#ProbationPeriod").val();
            instance[0].ProbationPeriod = shiyongQi;
            setEmployeeType();
        });
        $("#JoinDate").mouseleave(function () { //
            if ($("#JoinDate").val() == "") return;
            setEmployeeType();
        });
        var deptName = getPar(decodeURI(location.href), "deptName");
        var empType = getPar(decodeURI(location.href), "type"); //员工所属类型 1.在职，2.不在职，3.离职,4.离退休
        $("#txtDepartmentID").val(deptName); //获取部门名称
        if (getPar(location.href, "id") == "") { //新增
            var deptId = getPar(location.href, "deptId");
            $("#DepartmentID").val(deptId); //获取部门ID
            GetPostions(deptId); //获取部门岗位信息       
            instance[0].DepartmentID = deptId;
            instance[0].EmployeeStatus = empType;
            if (empType == "1") { //在职管理时工号自动获取，不在职，离职和离退休工号自动输入
                GetSequenceNum(deptId); //获取工号
                //$("#EmployeeCode").attr("readonly", "readonly");
            }
            //民族以汉族为默认项
            $("#Nationality").val("汉族");
            //最高学历默认为本科
            $("#TopDegree").val("BenKe");
            //试用期限默认2个月
            $("#ProbationPeriod").val("7D43BF0D-6942-4997-B19F-2079D86ACF1F".toLocaleLowerCase());
        }
        else {   //修改
            var postName = getPar(decodeURI(location.href), "postName"); //岗位名称
            //GetDeptIDByPostName(postName); //获取部门ID
            var deptId = instance[0].DepartmentID; //$("#DepartmentID").val();
            $("#DepartmentID").val(deptId); //获取部门ID
            GetPostions(deptId); //获取部门岗位信息 
            if ($("#BirthDate").val() <= "1900-01-01 ") {
                $("#BirthDate").val("");
            }
            if ($("#BirthDate").val()!="") $("#age").val(getAgeByBirthday($("#BirthDate").val())); //获取年龄
            $("#ProbationPeriod").attr("value", instance[0].ProbationPeriod); //试用期限
            if ($("#EmployeeCode").val() != "") { //当工号为空时才可修改,不为空不能修改
                //$("#EmployeeCode").attr("readonly", "readonly"); //工号不允许修改
            }
            $("#TopDegree").val(instance[0].TopDegree); //学历
            $("#PositionLevel").attr("value", instance[0].PositionLevel); //职等
            $("#Nationality").attr("value", instance[0].Nationality); //民族
            $("#MaritalStatus").attr("value", instance[0].MaritalStatus); //婚姻
            $("#BloodType").attr("value", instance[0].BloodType); //血型
            $("#ResumeSource").attr("value", instance[0].ResumeSource); //招聘来源
            if ($("#JoinJobDate").val() <= "1900-01-01 ") {
                $("#JoinJobDate").val("");
            }
        }
        //        $("select").each(function () {
        //            var optionId = $(this).attr(id);
        //            $(this).find("option").each(function () {
        //                if ($(this).val() == "") {
        //                    $(this).remove();
        //                }
        //            });
        //        });
    });
}

var ValueChanged = function () { //部门发生变化后
    var deptId = $("#DepartmentID").val(); 
    GetPostions(deptId); //获取该部门下所有岗位
    instance[0].DepartmentID = deptId; //调整后部门ID
}


//获取部门名称
function GetDeptNameByID(deptID) {
    var url = "../Extend/AjaxData/jsonlist.aspx?entity=DatDepartment&condition=DeptID='" + deptID + "'";
    $.get(url, function (d, s) {
        var list = eval("(" + d + ")");
        if (list.total == 0) {
            $("#tishixinxi").text("该部门无岗位信息，请先维护部门岗位信息！");
        }
        else {
            for (var o in list.rows) {
                var t = list.rows[o].DeptName;
                if (typeof (t) != "undefined") {
                    $("#DeptName").val(t);
                }
            }
        }
    });
}

//获取部门下的岗位
function GetPostions(deptID) {
    $("#PostID").find('option').remove();
    $("#tishixinxi").text("");
    var url = "../Extend/AjaxData/jsonlist.aspx?entity=DatPost&condition=DeptID='" + deptID + "'";
    $.get(url, function (d, s) {
        var list = eval("(" + d + ")");
        if (list.total == 0) {
            $("#tishixinxi").text("该部门无岗位信息，请先维护部门岗位信息！");
        }
        else {
            for (var o in list.rows) {
                var v = list.rows[o].PostID;
                var t = list.rows[o].PostName;
                if (typeof (t) != "undefined") {
                    $("#PostID").append("<option value='" + v + "' title='" + t + "'>" + t + "</option>");
                }
            }
            if (getPar(location.href, "id") != "") { //修改时
                $("#PostID").attr("value", instance[0].PostID); //岗位
            }
        }
    });
}

//通过岗位名称获取部门ID
function GetDeptIDByPostName(PostName) {
    var url = encodeURI("../Extend/AjaxData/jsonlist.aspx?entity=DatPost&condition=PostName='" + PostName + "'");
    $.get(url, function (d, s) {
        var list = eval("(" + d + ")");
        if (list.total == 0) {
            $("#tishixinxi").text("部门ID获取失败！");
        }
        else {
            for (var o in list.rows) {
                var v = list.rows[o].DeptID;
                if (typeof (v) != "undefined") {
                    $("#DepartmentID").val(v); //获取部门ID
                    break;
                }
            }
        }
    });
}

//获取工号
function GetSequenceNum(deptId)
{
        YAHOO.util.Connect.asyncRequest('POST', "../EHR/GetSequenceNum.aspx?deptId=" + deptId, {
        success: function (res) {
            try {
                var str = res.responseText;
                if (str == "-1") {
                    $("#tishixinxi").text("工号分配失败，原因为该部门工号未分配，请先进行部门工号分配！");
                }
                else {
                    if (str.substring(0, 1) == "e")//后台程序出现异常
                    {
                        $("#tishixinxi").text("获取工号失败：\r\n" + str);
                    }
                    else {
                        $("#EmployeeCode").val(str);
                    }
                }
            }
            catch (e) {
            }
            finally {
            }
        },
        failure: function (error) {
            alert("数据错误：" + error.statusText);
        }
    }, null);
}

var fnCheckData = function () {
    try {
        if ($("#EmployeeCode").val() == "") {
            return "员工工号不能为空！";
        }
        if ($("#CName").val() == "") {
            return "请输入员工姓名！";
        }
        if ($("#IDCardNO").val() == "") {
            return "请输入身份证号码！";
        }
        if ($("#txtDepartmentID").val() == "") {
            return "部门不能为空！";
        }
        if ($("#PostID").find("option:selected").text() == "") {
            return "岗位不能为空！";
        }
        if ($("#JoinDate").val() == "") {
            return "入职日期不能为空！";
        }
        if ($("#PhoneNo").val() == "") {
            return "联系电话不能为空！";
        }
        //身份证号码验证
        var idCard = $("#IDCardNO").val();
        var checkInfo = CheckIDCard(idCard);
        if (checkInfo != "0")//验证不通过
        {
            var flag = checkTaiWanIdCard(idCard);
            if (!flag) {
                return checkInfo; //"身份证号码输入有误，请输入15位或18位有效身份证号码!";
            }
        }
        //邮箱验证
        var email = $("#Email").val();
        if (email != "") {
            var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
            flag = pattern.test(email);
            if (!flag) {
                return "Email格式类似为:name@domain.com,请检查！";
            }
        }
        parent.SaveFlag = true;
    } catch (e)
     { }
}
